/*
 * Copyright Hundsun Technologies Inc. All Rights Reserved.
 */

package p2p

// register the Client driver
import (
	"fmt"

	"hundsun.com/hsl/hschain/common/types"

	"hundsun.com/hsl/hschain/module"
)

type createFunc func() module.Module

var p2pDrivers = make(map[string]createFunc)

// Register 注册 p2p driver
func Register(name string, create createFunc) {
	if create == nil {
		panic("p2p: Register driver is nil")
	}
	if _, dup := p2pDrivers[name]; dup {
		panic(fmt.Sprintf("p2p: driver %s has Registered", name))
	}
	p2pDrivers[name] = create
}

// Load 根据name的名称创建不同类型p2p
func load(name string) (createFunc, error) {
	if create, ok := p2pDrivers[name]; ok {
		return create, nil
	}
	return nil, types.ErrNotFound
}
